function [points_arr]=lines_intersections(a_b)

% This function returns all the intersection points between all lines

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% a_b - 2 coef array of a and b, describes a line as ax+b
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  points_arr -  array of all intersection points - x and y
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

count=length(a_b(1,:));
cnt_points=0;
temp_points_arr=zeros(2,count*(count-1)/2); % amount of all points is n*(n-1)/2

% running through all point with all other points
for i=1:count-1
    a1=a_b(1,i);
    b1=a_b(2,i);
    for j=i+1:count
        a2=a_b(1,j);
        b2=a_b(2,j);
        if (abs(a1-a2)>1e-10)  % if we have an intersection point
            if (a1~=inf) && (a2~=inf)
                x=(b2-b1)/(a1-a2);
                y=a1*x+b1;
            else
                if (a1==inf)
                    x=b1;
                    y=a2*b1+b2;
                else
                    x=b2;
                    y=a1*b2+b1;
                end
            end
            % inserting the point to the array
            cnt_points=cnt_points+1;
            temp_points_arr(1,cnt_points)=x;
            temp_points_arr(2,cnt_points)=y;
       end
    end
end
 
if (cnt_points==0)
    points_arr=[];
else 
    points_arr=temp_points_arr(1:2,1:cnt_points);
end
    